﻿using System;
using System.Collections.Generic;
using System.Text;
using GeneratorLibrary.Client.Base;

namespace GeneratorLibrary.Client.iSeries {
    public class iSeriesCrudTable : CrudTableBase {

        public override CrudColumnBase NewColumn(System.Data.DataRow dr) {
            return new iSeriesCrudColumn(dr);
        }

        protected override void WriteGrant(string storedProcedure) {
            /* TODO: add grant */

            StringBuilder sb = new StringBuilder();
            if (this.grantAccess.Length > 0) {
                sb.Append("GRANT EXECUTE ON ");
                sb.Append(this.tableName);
                sb.Append(storedProcedure);
                sb.Append(" TO ");
                sb.Append(this.grantAccess);
            }
            this.currentGrantSql = sb.ToString();
        }
        protected override void WriteComments(StringBuilder sb, string description) {
            sb.Append("\t--");
            sb.Append("\r\n\t-- Author: ");
            sb.Append(author);
            sb.Append("\r\n\t-- Create date: ");
            sb.Append(DateTime.Now.ToShortDateString());
            sb.Append("\r\n\t-- Description: ");
            sb.Append(description);
            sb.Append("\r\n\t-- Revisions: ");
            sb.Append("\r\n\t-- \r\n");
        }
        protected override bool DeclareColumn(StringBuilder sb, CrudColumnBase c, bool first, bool output, bool setToNull) {
            if (!first) {
                sb.Append(",\r\n");
            } else {
                first = !first;
            }

            sb.Append("\t");
            if (output)
                sb.Append("OUT ");
            else
                sb.Append("IN ");
            sb.Append(GetParameterPrefix(output));
            sb.Append(c.Name + " ");

            sb.Append(c.QualifiedDataType);

            return first;
        }
        protected override void CloseCRUDStatement(StringBuilder sb) {
            sb.Append(";\r\n");
        }
        protected override void SelectIdentityColumn(StringBuilder sb, string identityName) {
            sb.Append("\r\n\r\n\tset ");
            sb.Append(GetParameterPrefix(true));
            sb.Append(identityName);
            sb.Append(" = IDENTITY_VAL_LOCAL();");
        }
        protected override void EndMethodBody(StringBuilder sb, bool hasResultset) {
            if (hasResultset)
                sb.Append("\r\n\tOPEN cursor1;");
    
            sb.Append("\r\nEnd P1;\r\n");
        }

        protected override void StartMethodBody(StringBuilder sb, bool hasResultset) {
            StartMethodBody(sb, hasResultset, "");
        }
        protected override void StartMethodBody(StringBuilder sb, bool hasResultset, string comments) {
            sb.Append("\r\n)");
            if (hasResultset)
                sb.Append("\r\n\tRESULT SETS 1\r\n\tLANGUAGE SQL\r\n\tREADS SQL DATA");
            else
                sb.Append("\r\n\tLANGUAGE SQL\r\n\tMODIFIES SQL DATA");

            sb.Append("\r\nP1:Begin\r\n");

            WriteComments(sb, comments);

            if (hasResultset)
                sb.Append("DECLARE cursor1 CURSOR WITH RETURN FOR\r\n\r\n");
        }

        protected override void PrintTablePrefix(StringBuilder sb) {
            sb.Append(Database + ".");
            //no prefix
        }

        protected override void CreateProcedureDeclaration(StringBuilder sb, string procName) {
            sb.Append("Create Procedure ");
            sb.Append(ProcDatabase + ".");
            sb.Append(procName);
            sb.Append("(\r\n");
        }

        protected override ProcLocation GetWhereToPutComments() {
            return ProcLocation.BeforeMethodBody;
        }

        protected override string GetParameterPrefix(bool isOutput) {
            return isOutput ? "o_" : "i_";
        }
    }
}
